package com.vic.controller;

import com.vic.entity.Article;
import com.vic.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/route")
public class RouteController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/{path}")
    public String route(@PathVariable("path") String path,
                        @RequestParam(value = "id", required = false) String id,
                        Model model) {
        if ("index".equals(path)) {
            List<Article> articleList = articleService.getArticles();
            model.addAttribute("articleList", articleList);
        }
        if ("detail".equals(path)) {
            Article article = articleService.getArticle(id);
            model.addAttribute("content", article.getContent());
            model.addAttribute("articleId", article.getId());
        }
        if ("edit".equals(path)) {
            Article article = articleService.getArticle(id);
            model.addAttribute("article", article);
        }
        return path;
    }
}
